import cv2
import subprocess

def get_camera_info(device_serial):
    # 尝试打开手机摄像头
    cmd = f"adb -s {device_serial} shell \"am start -a android.media.action.STILL_IMAGE_CAMERA\""
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    output, _ = process.communicate()

    # 使用 adb shell 命令打开相机，并将输出通过 cv2.VideoCapture() 函数读取
    cap = cv2.VideoCapture(f"adb -s {device_serial} shell screencap -p")

    # 检查摄像头是否成功打开
    if not cap.isOpened():
        print(f"无法打开设备 {device_serial} 的摄像头。")
        return

    # 获取摄像头参数
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    zoom = cap.get(cv2.CAP_PROP_ZOOM)  # 此处尝试获取变焦倍数

    # 打印摄像头信息
    print(f"设备 {device_serial} 的摄像头参数:")
    print("分辨率:", width, "x", height)
    print("帧率:", fps)
    print("变焦倍数:", zoom)

    # 释放摄像头
    cap.release()


# 示例用法
serial_number = "1B231FDEE001CS"
get_camera_info(serial_number)